<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>乐优商城--购物车页面</title>

    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-cart.css"/>
</head>

<body>

<div id="cartApp">

    <div class="top">
        <shortcut/>
    </div>

    <div class="cart py-container">
        <!--logoArea-->
        <div class="logoArea">
            <div class="fl logo"><span class="title">购物车</span></div>
        </div>

        <!--All goods-->
        <div class="allgoods">
            <h4>全部商品</h4>
            <div class="cart-main">
                <div class="yui3-g cart-th">
                    <div class="yui3-u-1-4"><input type="checkbox" v-model="selectedAll" @click="selectedAll=!selectedAll"/> 全部
                    </div>
                    <div class="yui3-u-1-4">商品</div>
                    <div class="yui3-u-1-8">单价（元）</div>
                    <div class="yui3-u-1-8">数量</div>
                    <div class="yui3-u-1-8">小计（元）</div>
                    <div class="yui3-u-1-8">操作</div>
                </div>

                <div class="cart-item-list">
                    <div class="cart-body">
                        <div class="cart-list">
                            <ul class="goods-list yui3-g" v-for="(cart,index) in carts" :key="index">
                                <div class="yui3-u-1-1" v-if="cart.saleable">
                                    <li class="yui3-u-1-24">
                                        <input type="checkbox" v-model="selected" :value="cart"/>
                                    </li>
                                    <li class="yui3-u-11-24">
                                        <div class="good-item">
                                            <div class="item-img"><img :src="cart.image" width="80px" height="80px"/>
                                            </div>
                                            <div class="item-msg">
												<span style="line-height:40px ">
                                                    {{cart.title}}<br>
                                                    <span v-for="(v,k) in JSON.parse(cart.ownSpec)">
                                                        {{k}}:{{v}}&nbsp;
                                                    </span>
												</span>
                                            </div>
                                        </div>
                                    </li>
                                    <li class="yui3-u-1-8">
                                        <span style="line-height:70px" class="price">
                                            <span v-if="cart.currentPrice<cart.price">
                                                <div style="font-size: 12px;color:red">比加入时降:{{ly.formatPrice(cart.price-cart.currentPrice)}}</div>
                                                <span style="color:red">{{ly.formatPrice(cart.currentPrice)}}</span>
                                            </span>
                                            <span v-if="cart.currentPrice>cart.price">
                                                <div style="font-size: 12px;color:red">比加入时涨:{{ly.formatPrice(cart.currentPrice-cart.price)}}</div>
                                                <span style="color:red">{{ly.formatPrice(cart.currentPrice)}}</span>
                                            </span>
                                            <span v-if="cart.currentPrice===cart.price">{{ly.formatPrice(cart.currentPrice)}}</span></span>
                                    </li>

                                    <li class="yui3-u-1-8" style="padding-top: 20px">
                                        <a href="javascript:void(0)" class="increment mins"
                                           @click="decrement(cart)">-</a>
                                        <input autocomplete="off" type="text" v-model="cart.num" minnum="1"
                                               class="itxt"/>
                                        <a href="javascript:void(0)" class="increment plus"
                                           @click="increment(cart)">+</a>
                                    </li>
                                    <li class="yui3-u-1-8">
                                        <span style="line-height:70px " class="sum">{{ly.formatPrice(cart.currentPrice*cart.num)}}</span>
                                    </li>
                                    <li class="yui3-u-1-12">
                                        <a href="#none" @click="deleteCart(index)">删除</a><br/>
                                        <a href="#none" @click="addCollect(index)">移到我的关注</a>
                                    </li>
                                </div>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <div class="cart-tool">
                <div class="select-all">
                    <input type="checkbox" v-model="selectedAll" @click="selectedAll=!selectedAll"/>
                    <span>全选</span>
                </div>
                <div class="option">
                    <a href="#none" @click="deleteSelectedCart()">删除选中的商品</a>
                    <a href="#none" @click="addCollectAll()">移到我的关注</a>
                    <a href="#none">清除下柜商品</a>
                </div>
                <div class="toolbar">
                    <div class="chosed">已选择<span>0</span>件商品</div>
                    <div class="sumprice">
                        <span><em>总价（不含运费） ：</em><i class="summoney">{{ly.formatPrice(totalPrice)}}</i></span>
                        <span><em>已节省：</em><i>-¥20.00</i></span>
                    </div>
                    <div class="sumbtn">
                        <a class="sum-btn" href="#" @click.prevent="toOrderInfo" target="_blank">结算</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="deled">
                <h3>已下架商品</h3>
                <div v-for="(cart,index) in carts">
                    <div class="cart-list unsaleable" v-if="!cart.saleable">
                        <ul class="goods-list yui3-g">
                            <li class="yui3-u-1-24">
                                <input type="checkbox" disabled/>
                            </li>
                            <li class="yui3-u-11-24">
                                <div class="good-item">
                                    <div class="item-img"><img :src="cart.image" width="80px" height="80px"/></div>
                                    <div class="item-msg">
												<span style="line-height:40px ">
                                                    {{cart.title}}<br>
												</span>
                                    </div>
                                </div>
                            </li>
                            <li class="yui3-u-1-8">
                                <span style="line-height:70px "
                                      class="price">{{ly.formatPrice(cart.currentPrice)}}</span>
                            </li>
                            <li class="yui3-u-1-8" style="padding-top: 20px">
                                <a href="javascript:void(0)" class="increment mins" @click="decrement(cart)">-</a>
                                <input autocomplete="off" type="text" class="itxt" style="text-align: center"
                                       :value="cart.num" disabled/>
                                <a href="javascript:void(0)" class="increment plus" @click="increment(cart)">+</a>
                            </li>
                            <li class="yui3-u-1-8">
                                <span style="line-height:70px " class="sum">{{ly.formatPrice(cart.currentPrice*cart.num)}}</span>
                            </li>
                            <li class="yui3-u-1-12">
                                <a href="#none" @click="deleteCart(index)">从购物车中删除</a><br/>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="deled">
                <h3>已删除商品，您可以重新购买或加关注：</h3>
                <div class="cart-list del" v-for="(deleteCart,index) in deleteCarts" :key="index">
                    <ul class="goods-list yui3-g">
                        <li class="yui3-u-1-2">
                            <div class="good-item">
                                <div class="item-msg">{{deleteCart.title}}</div>
                            </div>
                        </li>
                        <li class="yui3-u-1-6"><span class="price">{{ly.formatPrice(deleteCart.price)}}</span></li>
                        <li class="yui3-u-1-6">
                            <span class="number">{{deleteCart.num}}</span>
                        </li>
                        <li class="yui3-u-1-8">
                            <a href="#none" @click="againAddCart(index)">重新购买</a>
                            <a href="#none" @click="addCollect(index)">移到我的关注</a>
                        </li>
                    </ul>
                </div>
            </div>
            <!--猜你喜欢-->
            <div class="liked">
                <ul class="sui-nav nav-tabs">
                    <li class="active">
                        <a href="#index" data-toggle="tab">猜你喜欢</a>
                    </li>
                    <li>
                        <a href="#profile" data-toggle="tab">特惠换购</a>
                    </li>
                </ul>
                <div class="clearfix"></div>
                <div class="tab-content">
                    <div id="index" class="tab-pane active">
                        <div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
                            <div class="carousel-inner">
                                <div class="active item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
                            <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
                        </div>
                    </div>
                    <div id="profile" class="tab-pane">
                        <p>特惠选购</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<script src="./js/vue/vue.js"></script>
<script src="./js/axios.min.js"></script>
<script src="./js/common.js"></script>
<script type="text/javascript">
    var cartVm = new Vue({
        el: "#cartApp",
        data: {
            ly,
            carts: [],//购物车数据
            deleteCarts: [],//删除购物车数据
            selected: [], // 选中的商品
            deleted: [], // 被删除的商品
            selectedAll: true, //是否全选
            selectedToDeleteSkuIds:[]
        },
        created() {
            this.loadCarts();
        },
        methods: {
            toOrderInfo() {
                ly.verifyUser()
                    .then(() => {
                        //已登录   保存已选择的购物车数据到localStorage
                        ly.store.set("selectedCarts", this.selected);
                        window.location.href = "/getOrderInfo.html";
                    })
                    .catch(() => {
                        //未登录
                        window.location.href = "/login.html?returnUrl=" + window.location.href;
                    })
            },
            loadCarts() {
                // 先判断登录状态
                ly.verifyUser()
                    .then(() => {
                        const carts = ly.store.get("ly_carts");
                        if (carts) {
                            //合并购物车
                            ly.http.post("/cart/merge", carts)
                                .then(() => {
                                    ly.http.get("/cart")
                                        .then(({data}) => {
                                            this.carts = data;
                                            this.carts.forEach(cart => {
                                                if (cart.saleable) {
                                                    this.selected.push(cart);
                                                }
                                            });
                                        });
                                    this.deleteCarts = ly.sessionStore.get("delete_carts");
                                    ly.store.del("ly_carts");
                                });
                        } else {
                            ly.http.get("/cart")
                                .then(({data}) => {
                                    this.carts = data;
                                    this.carts.forEach(cart => {
                                        if (cart.saleable) {
                                            this.selected.push(cart);
                                        }
                                    });
                                });
                            this.deleteCarts = ly.sessionStore.get("delete_carts");
                        }
                    })
                    .catch(() => {
                        // 未登录购物车,查询
                        this.carts = ly.store.get("ly_carts") || [];
                        this.carts.forEach(cart => {
                            ly.http.get("/item/saleable?skuId=" + cart.skuId)
                                .then(({data}) => {
                                    cart.currentPrice = data.currentPrice;
                                    cart.saleable = data.saleable;
                                    if (cart.saleable) {
                                        this.selected.push(cart);
                                    }
                                });
                        });
                        this.deleteCarts = ly.sessionStore.get("delete_carts");
                    });
            },
            // 添加到我的关注
            addCollect(i) {
                ly.verifyUser()
                    .then(() => {
                        // 已登录
                        ly.http.post("/cart/addCollect", this.carts[i])
                            .then(() => {
                                this.carts.splice(i, 1);
                                window.location.href = "http://www.leyou.com/cart.html";
                            });
                    })
                    .catch(() => {
                        // 未登录,跳转登录页面
                        window.location.href = "http://www.leyou.com/login.html?returnUrl=http://www.leyou.com/cart.html";
                    });
            },
            // 将选中的商品移到我的收藏
            addCollectAll() {
                ly.verifyUser()
                    .then(() => {
                        // 已登录
                        carts = this.selected;
                        ly.http.post("/cart/addCollect/all", carts)
                            .then(() => {
                                //this.carts.splice(i, i);
                                window.location.href = "http://www.leyou.com/cart.html";
                            });
                    })
                    .catch(() => {
                        // 未登录,跳转登录页面
                        window.location = "login.html?returnUrl=" + window.location;
                    });
            },
            increment(cart) {
                if (cart) {
                    cart.num++;
                    this.updateNum(cart);
                }
            },
            decrement(cart) {
                if (cart && cart.num > 1) {
                    cart.num--;
                    this.updateNum(cart);
                }
            },
            updateNum(c) {
                ly.verifyUser()
                    .then(() => {
                        // 已登录，向后台发起请求
                        ly.http.put("/cart", ly.stringify({skuId: c.skuId, num: c.num}));
                    })
                    .catch(() => {
                        // 未登录，直接操作本地数据
                        ly.store.set("ly_carts", this.carts);
                    })
            },
            deleteSelectedCart(){
                for(let i=0;i<this.selected.length;i++){
                    this.selectedToDeleteSkuIds.push(this.selected[i].skuId);
                }

                ly.verifyUser()
                    .then(() => {
                        // 已登录
                        for(let k=0;k<this.selectedToDeleteSkuIds.length;k++){
                            for(let j=0;j<this.carts.length;j++){
                                if(this.carts[j].skuId===this.selectedToDeleteSkuIds[k]){
                                    ly.http.delete("/cart/" + this.carts[j].skuId);
                                    this.delete(j);
                                    j--;
                                }
                            }
                        }
                    })
                    .catch(() => {
                        // 未登录
                        for(let k=0;k<this.selectedToDeleteSkuIds.length;k++){
                            for(let j=0;j<this.carts.length;j++){
                                if(this.carts[j].skuId===this.selectedToDeleteSkuIds[k]){
                                    this.delete(j);
                                    // 重新将购物车写入localstorage
                                    ly.store.set("ly_carts",this.carts);
                                    j--;
                                }
                            }
                        }
                        this.selectedToDeleteSkuIds=[];

                    });
            },
            deleteCart(i) {
                ly.verifyUser()
                    .then(() => {
                        // 已登录
                        ly.http.delete("/cart/" + this.carts[i].skuId)
                            .then(() =>{
                                this.delete(i);
                            });
                    })
                    .catch(() => {
                        // 未登录
                        this.delete(i);
                        // 4、重新将购物车写入localstorage
                        ly.store.set("ly_carts",this.carts);

                    });
            },
            delete(i){
                // 1、删除页面中元素
                let spliceCart=this.carts.splice(i, 1);
                // 2、存入SessionStorage中
                // 2.1、查询SessionStorage购物车
                let sessionCarts = ly.sessionStore.get("delete_carts") || [];
                let sessionCart = sessionCarts.find(sc => sc.skuId === spliceCart[0].skuId);
                // 2.2、判断是否存在
                if(sessionCart){
                    // 2.2.1、存在，修改数量
                    sessionCart.num+=spliceCart[0].num;
                }else{
                    // 2.2.2、不存在，新增一条删除购物车记录
                    sessionCarts.push(spliceCart[0]);
                }
                // 2.3、将sessionCarts写入SessionStorage
                ly.sessionStore.set("delete_carts",sessionCarts);

                // 3、赋值deleteCarts
                this.deleteCarts=sessionCarts;
            },
            /*deleteCart(i) {
                ly.verifyUser()
                    .then(() => {
                        // 已登录
                        ly.http.delete("/cart/" + this.carts[i].skuId)
                            .then(() => {
                                // 1、删除页面中元素
                                let spliceCart = this.carts.splice(i, 1);

                                // 2、存入SessionStorage中
                                // 2.1、查询SessionStorage购物车
                                let sessionCarts = ly.sessionStore.get("delete_carts") || [];
                                let sessionCart = sessionCarts.find(sc => sc.skuId === spliceCart[0].skuId);

                                // 2.2、判断是否存在
                                if (sessionCart) {
                                    // 2.2.1、存在，修改数量
                                    sessionCart.num += spliceCart[0].num;
                                } else {
                                    // 2.2.2、不存在，新增一条删除购物车记录
                                    sessionCarts.push(spliceCart[0]);
                                }

                                // 2.3、将sessionCarts写入SessionStorage
                                ly.sessionStore.set("delete_carts", sessionCarts);

                                // 3、赋值deleteCarts
                                this.deleteCarts = sessionCarts;
                            });
                    })
                    .catch(() => {
                        // 未登录
                        // 1、删除页面中元素
                        let spliceCart = this.carts.splice(i, 1);

                        // 2、存入SessionStorage中
                        // 2.1、查询SessionStorage购物车
                        let sessionCarts = ly.sessionStore.get("delete_carts") || [];
                        let sessionCart = sessionCarts.find(sc => sc.skuId === spliceCart[0].skuId);

                        // 2.2、判断是否存在
                        if (sessionCart) {
                            // 2.2.1、存在，修改数量
                            sessionCart.num += spliceCart[0].num;
                        } else {
                            // 2.2.2、不存在，新增一条删除购物车记录
                            sessionCarts.push(spliceCart[0]);
                        }

                        // 2.3、将sessionCarts写入SessionStorage
                        ly.sessionStore.set("delete_carts", sessionCarts);

                        // 3、赋值deleteCarts
                        this.deleteCarts = sessionCarts;

                        // 4、重新将购物车写入localstorage
                        ly.store.set("ly_carts", this.carts);

                    });
            },*/
            againAddCart(i) {
                ly.verifyUser()
                    .then(resp => {
                        //已登录,发送信息到后台,添加到redis
                        ly.http.post("/cart", this.deleteCarts[i])
                            .then(resp => {
                                // 1、获取需要重新购买的购物车
                                this.deleteCarts.splice(i, 1);

                                // 2、重新将购物车写入SessionStorage
                                ly.sessionStore.set("delete_carts", this.deleteCarts);

                                this.loadCarts();
                            });
                    }).catch(() => {
                    // 未登录
                    // 1、获取需要重新购买的购物车
                    let againAddCart = this.deleteCarts.splice(i, 1);

                    // 2、存入localstorage
                    // 2.1、查询localstorage购物车
                    let localCarts = ly.store.get("ly_carts") || [];
                    let localCart = localCarts.find(c => c.skuId === againAddCart[0].skuId);

                    // 2.2.判断是否存在
                    if (localCart) {
                        ly.http.get("/item/saleable?skuId=" + localCart.skuId)
                            .then(({data}) => {
                                localCart.saleable = data.saleable;
                                localCart.currentPrice = data.currentPrice;

                                // 2.2.1.存在,修改数量
                                localCart.num += againAddCart[0].num;

                                // 2.3、将localCarts写回localstorage
                                ly.store.set("ly_carts", localCarts);

                                // 3、赋值carts
                                this.carts = localCarts;

                                // 4、重新将购物车写入SessionStorage
                                ly.sessionStore.set("delete_carts", this.deleteCarts);
                            });
                    } else {
                        // 2.2.2.不存在,新增一条购物车记录
                        localCarts.push(againAddCart[0]);

                        // 2.3、将localCarts写回localstorage
                        ly.store.set("ly_carts", localCarts);

                        // 3、赋值carts
                        this.carts = localCarts;

                        // 4、重新将购物车写入SessionStorage
                        ly.sessionStore.set("delete_carts", this.deleteCarts);
                    }
                });
            },
           /* selectedCarts(){
                if (!this.selectedAll) {
                    this.selectedAll=true;
                    this.carts.forEach(cart => {
                        if (cart.saleable) {
                            this.selected.push(cart);
                        }
                    })
                }else{
                    this.selectedAll=false;
                }
            },
            selectedCart(){
                if (this.selectedAll) {
                    this.selectedAll=false;
                }
            }*/
        },
        computed: {
            totalPrice() {
                return this.selected.map(c => c.num * c.currentPrice).reduce((p1, p2) => p1 + p2, 0);
            }
        },
        components: {
            shortcut: () => import("/js/pages/shortcut.js")
        },
        watch: {
            /*selected:{
                handler() {
                    if(this.carts.length==this.selected.length){
                        this.selectedAll=true;
                    }
                }
            },*/
            selectedAll: {
                handler() {
                    if(this.selectedAll){
                        this.selected=this.carts;
                        this.carts.forEach(cart => {
                            if (cart.saleable) {
                                this.selected.push(cart);
                            }
                        })
                    }else{
                        this.selected=[];
                    }
                }
            },
            deleteCarts: {
                deep: true,
                handler(val, old) {
                    if (!old || !old.key) {
                        // 如果旧的deleteCarts值为空，或者deleteCarts中的key为空，证明是第一次
                        return;
                    }
                    // 把deleteCarts对象变成请求参数，拼接在url路径
                    location.href = "http://www.leyou.com/cart.html";
                }
            }
        }
    })
</script>
<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/widget/nav.js"></script>
</body>
</html>